package com.am.service.serviceImpl;

import com.am.bean.Menu;
import com.am.bean.MenuRole;
import com.am.dao.MenuMapper;
import com.am.dao.MenuRoleMapper;
import com.am.service.MenuCheckService;
import com.utils.Meg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuCheckServiceImpl implements MenuCheckService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private MenuRoleMapper menuRoleMapper;

    public List getMenuJsonDate(Integer id){
        List menuIdList = menuRoleMapper.selectByRoleId(id);
        return getMenuJson(0,menuIdList);
    }

    @Override
    public Meg menuCheck(Integer roleId, Integer[] menuId) {
        menuRoleMapper.deleteByRoleId(roleId);
        for (int i = 0; i < menuId.length; i++) {
            MenuRole menuRole = new MenuRole();
            menuRole.setMenuId(menuId[i]);
            menuRole.setRoleId(roleId);
            menuRoleMapper.insert(menuRole);
        }
        return Meg.success();
    }

    public List getMenuJson(Integer id,List menuIdList){
        List list = new ArrayList();
        List<Menu> menuList = menuMapper.getMenuByParentId(id);
        Map map = null;
        for (int i = 0; i < menuList.size() ; i++) {
            map = new  HashMap();
            map.put("id",menuList.get(i).getId());
            map.put("title",menuList.get(i).getMenuName());
            map.put("disabled",menuList.get(i).getMenuActivate().equals("on")?false:true);
            map.put("checked",menuIdList.contains(menuList.get(i).getId())?true:false);
            if(getMenuJson(menuList.get(i).getId(),menuIdList).size()>0){
                map.put("children",getMenuJson(menuList.get(i).getId(),menuIdList));
            }
            list.add(map);
        }
        return list;
    }



}
